package zry.Operate;

import zry.Achievement.*;
import zry.Achievement.Thesis.*;
import zry.Achievement.Textbook.*;
import zry.Achievement.Standard.*;
import zry.Achievement.Reward.*;
import zry.Achievement.Report.*;
import zry.Achievement.Platform.*;
import zry.Achievement.Patent.*;
import zry.Achievement.Other.*;
import zry.Search.*;
import zry.User.Teacher.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

//查询成果认定申请
public class Search {
    //学生获取本人的成果认定记录
    //获取id为userid的所有成果认定情况
    public void searchAllMyAchievement(String userid) {
        AchievementDAO achievementDAO = new AchievementDAOImpl();
        SearchCriteriaAchievement searchCriteriaAchievement = new SearchCriteriaAchievement();
        searchCriteriaAchievement.setS_id(userid);
        List<Achievement> achievements = achievementDAO.findAchievements(searchCriteriaAchievement);
        System.out.println("成果id\t\t成果类型\t\t用户id\t\t初审结果\t\t终审结果");
        System.out.println(" " + achievements.toString().replaceAll("\\[", "").replaceAll(",", "").replaceAll("]", ""));
    }

    //学生获取本人的某一条成果认定记录具体情况
    public void searchMyAchievement(String userid) {
        AchievementDAO achievementDAO = new AchievementDAOImpl();
        SearchCriteriaAchievement searchCriteriaAchievement = new SearchCriteriaAchievement();
        searchCriteriaAchievement.setS_id(userid);
        List<Achievement> achievements = achievementDAO.findAchievements(searchCriteriaAchievement);
        System.out.println("成果id\t\t成果类型\t\t用户id\t\t初审结果\t\t终审结果");
        System.out.println(" " + achievements.toString().replaceAll("\\[", "").replaceAll(",", "").replaceAll("]", ""));

        //将本人的成果认定申请id存入一个list，用户只能查看本人的申请
        List<String> achievementIds = new ArrayList<>();
        for (Achievement a : achievements) {
            achievementIds.add(a.getA_id());
        }

        System.out.println("请输入要查看的成果id（输入-1退出）");
        Scanner scanner = new Scanner(System.in);
        while (true) {
            String input = scanner.nextLine();
            if (input.equals("-1")) break;
            //只有输入的成果id在本人的list中，才能正确显示
            if (achievementIds.contains(input)) {
                Achievement achievement = achievementDAO.getAchievement(input);
                System.out.println("成果id\t\t成果类型\t\t用户id\t\t初审结果\t\t终审结果");
                System.out.println(achievement);
                switch (achievement.getA_type()) {
                    case "论文":
                        ThesisDAO thesisDAO = new ThesisDAOImpl();
                        Thesis thesis = thesisDAO.getThesis(input);
                        System.out.println(thesis);
                        break;
                    case "教材":
                        TextbookDAO textbookDAO = new TextbookDAOImpl();
                        Textbook textbook = textbookDAO.getTextbook(input);
                        System.out.println(textbook);
                        break;
                    case "标准":
                        StandardDAO standardDAO = new StandardDAOImpl();
                        Standard standard = standardDAO.getStandard(input);
                        System.out.println(standard);
                        break;
                    case "奖励":
                        RewardDAO rewardDAO = new RewardDAOImpl();
                        Reward reward = rewardDAO.getReward(input);
                        System.out.println(reward);
                        break;
                    case "报告":
                        ReportDAO reportDAO = new ReportDAOImpl();
                        Report report = reportDAO.getReport(input);
                        System.out.println(report);
                        break;
                    case "平台":
                        PlatformDAO platformDAO = new PlatformDAOImpl();
                        Platform platform = platformDAO.getPlatform(input);
                        System.out.println(platform);
                        break;
                    case "专利":
                        PatentDAO patentDAO = new PatentDAOImpl();
                        Patent patent = patentDAO.getPatent(input);
                        System.out.println(patent);
                        break;
                    case "其他":
                        OtherDAO otherDAO = new OtherDAOImpl();
                        Other other = otherDAO.getOther(input);
                        System.out.println(other);
                        break;
                }
                break;
            } else {
                System.out.println("成果id输入错误，请输入本人的成果id（输入-1退出）");
            }
        }
    }

    //导师获取自己学生的所有成果认定记录
    //先利用findStudents获取自己所有学生的id，再查找这些id的成果认定情况
    public void searchAllMyStudentsAchievement(String userid) {
        TeacherDAO teacherDAO = new TeacherDAOImpl();
        List<String> studentIds = teacherDAO.findStudents(userid);

        AchievementDAO achievementDAO = new AchievementDAOImpl();
        SearchCriteriaAchievement searchCriteriaAchievement = new SearchCriteriaAchievement();
        List<Achievement> achievements = new ArrayList<>();

        for (String s : studentIds) {
            searchCriteriaAchievement.setS_id(s);
            achievements.addAll(achievementDAO.findAchievements(searchCriteriaAchievement));
        }

        System.out.println("成果id\t\t成果类型\t\t学生id\t\t初审结果\t\t终审结果");
        System.out.println(" " + achievements.toString().replaceAll("\\[", "").replaceAll(",", "").replaceAll("]", ""));
    }

    //导师查看自己学生的某一条成果认定记录
    //先获取自己所有学生的id，再查找这些id的成果认定情况，获取所有成果id
    public void searchMyStudentsAchievement(String userid) {
        TeacherDAO teacherDAO = new TeacherDAOImpl();
        List<String> studentIds = teacherDAO.findStudents(userid);

        AchievementDAO achievementDAO = new AchievementDAOImpl();
        SearchCriteriaAchievement searchCriteriaAchievement = new SearchCriteriaAchievement();
        List<Achievement> achievements = new ArrayList<>();

        //获取自己所有学生的id，查找这些id的成果认定情况
        for (String s : studentIds) {
            searchCriteriaAchievement.setS_id(s);
            achievements.addAll(achievementDAO.findAchievements(searchCriteriaAchievement));
        }
        System.out.println("成果id\t\t成果类型\t\t学生id\t\t初审结果\t\t终审结果");
        System.out.println(" " + achievements.toString().replaceAll("\\[", "").replaceAll(",", "").replaceAll("]", ""));

        //将学生的成果认定申请id存入一个list，用户只能查看自己学生的成果id
        List<String> achievementIds = new ArrayList<>();
        for (Achievement a : achievements) {
            achievementIds.add(a.getA_id());
        }
        System.out.println("请输入要查看的成果id（输入-1退出）");
        Scanner scanner = new Scanner(System.in);
        while (true) {
            String input = scanner.nextLine();
            if (input.equals("-1")) break;
            //只有输入的成果id在list中，才能正确显示
            if (achievementIds.contains(input)) {
                Achievement achievement = achievementDAO.getAchievement(input);
                System.out.println("成果id\t\t成果类型\t\t用户id\t\t初审结果\t\t终审结果");
                System.out.println(achievement);
                switch (achievement.getA_type()) {
                    case "论文":
                        ThesisDAO thesisDAO = new ThesisDAOImpl();
                        Thesis thesis = thesisDAO.getThesis(input);
                        System.out.println(thesis);
                        break;
                    case "教材":
                        TextbookDAO textbookDAO = new TextbookDAOImpl();
                        Textbook textbook = textbookDAO.getTextbook(input);
                        System.out.println(textbook);
                        break;
                    case "标准":
                        StandardDAO standardDAO = new StandardDAOImpl();
                        Standard standard = standardDAO.getStandard(input);
                        System.out.println(standard);
                        break;
                    case "奖励":
                        RewardDAO rewardDAO = new RewardDAOImpl();
                        Reward reward = rewardDAO.getReward(input);
                        System.out.println(reward);
                        break;
                    case "报告":
                        ReportDAO reportDAO = new ReportDAOImpl();
                        Report report = reportDAO.getReport(input);
                        System.out.println(report);
                        break;
                    case "平台":
                        PlatformDAO platformDAO = new PlatformDAOImpl();
                        Platform platform = platformDAO.getPlatform(input);
                        System.out.println(platform);
                        break;
                    case "专利":
                        PatentDAO patentDAO = new PatentDAOImpl();
                        Patent patent = patentDAO.getPatent(input);
                        System.out.println(patent);
                        break;
                    case "其他":
                        OtherDAO otherDAO = new OtherDAOImpl();
                        Other other = otherDAO.getOther(input);
                        System.out.println(other);
                        break;
                }
                break;
            } else {
                System.out.println("成果id输入错误，请输入您的学生的成果id（输入-1退出）");
            }
        }
    }

    //管理员获取所有成果认定记录
    public void searchAllAchievement() {
        AchievementDAO achievementDAO = new AchievementDAOImpl();
        List<Achievement> achievements = achievementDAO.getAllAchievements();

        System.out.println("成果id\t\t成果类型\t\t学生id\t\t初审结果\t\t终审结果");
        System.out.println(" " + achievements.toString().replaceAll("\\[", "").replaceAll(",", "").replaceAll("]", ""));
    }

    //管理员查看某一条成果认定记录
    public void searchAchievement() {
        AchievementDAO achievementDAO = new AchievementDAOImpl();
        List<Achievement> achievements = achievementDAO.getAllAchievements();

        System.out.println("成果id\t\t成果类型\t\t学生id\t\t初审结果\t\t终审结果");
        System.out.println(" " + achievements.toString().replaceAll("\\[", "").replaceAll(",", "").replaceAll("]", ""));

        List<String> achievementIds = new ArrayList<>();
        for (Achievement a : achievements) {
            achievementIds.add(a.getA_id());
        }

        System.out.println("请输入要查看的成果id（输入-1退出）");
        Scanner scanner = new Scanner(System.in);
        while (true) {
            String input = scanner.nextLine();
            if (input.equals("-1")) break;
            //只有输入的成果id在list中，才能正确显示
            if (achievementIds.contains(input)) {
                Achievement achievement = achievementDAO.getAchievement(input);
                System.out.println("成果id\t\t成果类型\t\t用户id\t\t初审结果\t\t终审结果");
                System.out.println(achievement);
                switch (achievement.getA_type()) {
                    case "论文":
                        ThesisDAO thesisDAO = new ThesisDAOImpl();
                        Thesis thesis = thesisDAO.getThesis(input);
                        System.out.println(thesis);
                        break;
                    case "教材":
                        TextbookDAO textbookDAO = new TextbookDAOImpl();
                        Textbook textbook = textbookDAO.getTextbook(input);
                        System.out.println(textbook);
                        break;
                    case "标准":
                        StandardDAO standardDAO = new StandardDAOImpl();
                        Standard standard = standardDAO.getStandard(input);
                        System.out.println(standard);
                        break;
                    case "奖励":
                        RewardDAO rewardDAO = new RewardDAOImpl();
                        Reward reward = rewardDAO.getReward(input);
                        System.out.println(reward);
                        break;
                    case "报告":
                        ReportDAO reportDAO = new ReportDAOImpl();
                        Report report = reportDAO.getReport(input);
                        System.out.println(report);
                        break;
                    case "平台":
                        PlatformDAO platformDAO = new PlatformDAOImpl();
                        Platform platform = platformDAO.getPlatform(input);
                        System.out.println(platform);
                        break;
                    case "专利":
                        PatentDAO patentDAO = new PatentDAOImpl();
                        Patent patent = patentDAO.getPatent(input);
                        System.out.println(patent);
                        break;
                    case "其他":
                        OtherDAO otherDAO = new OtherDAOImpl();
                        Other other = otherDAO.getOther(input);
                        System.out.println(other);
                        break;
                }
                break;
            } else {
                System.out.println("成果id输入错误，请输入正确的成果id（输入-1退出）");
            }
        }
    }
}
